<?php

namespace App\Console\Commands;

use App\Jobs\TempPush;
use App\Models\Appoint;
use App\Models\ArticleType;
use App\Models\Family;
use App\Models\FamilyEdu;
use App\Models\FamilyType;
use App\Models\Hospital;
use App\Models\HospitalAdministrator;
use App\Models\HospitalAdminUserHospital;
use App\Models\HospitalTeam;
use App\Models\HospitalUserFamily;
use App\Models\old\Article;
use App\Models\old\ArticleInfo;
use App\Models\old\Autograph;
use App\Models\old\DoctorHospital;
use App\Models\old\DoctorParent;
use App\Models\old\HospitalAdminRoleUsers;
use App\Models\old\HospitalAdminUser;
use App\Models\old\Interview;
use App\Models\old\OldDoctor;
use App\Models\old\OldHospital;
use App\Models\old\OldHospitals;
use App\Models\old\Pregnancy;
use App\Models\old\UserLogin;
use App\Models\TempPushRecord;
use App\Models\TempUserid;
use App\Wechat\MyAccessToken;
use App\Wechat\Temp;
use Illuminate\Console\Command;

class migrationSign extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migration:sign';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $userids = TempUserid::query()->pluck('userid')->toArray();

        $doctorParents = DoctorParent::query()
            ->whereIn('parentid',$userids)
            ->get();
        foreach($doctorParents as $doctorParent){
            echo $doctorParent->id;
            echo "\n";
            $familys = Family::query()->where('user_id',$doctorParent->parentid)->get();
            foreach($familys as $family) {
                $row['user_id'] = $doctorParent->parentid;
                $row['family_id'] = $family->id;
                if(!$family->team_id){
                    $team = HospitalTeam::query()->where('hospital_id',$doctorParent->doctorid)->first();
                    $team_id = $team->id;
                }else{
                    $team = HospitalTeam::query()
                        ->where('id',$family->team_id)
                        ->first();
                    if(isset($team) && $team->hospital_id == $doctorParent->parentid){
                        $team_id = $family->team_id;
                    }else{
                        $team = HospitalTeam::query()->where('hospital_id',$doctorParent->doctorid)->first();
                        $team_id = $team->id;
                    }
                }
                $row['hospital_team_id'] = isset($team_id)?$team_id:0;
                $row['hospital_id'] = $doctorParent->doctorid;
                $row['created_at'] = date('Y-m-d H:i:s', $doctorParent->createtime);
                $row['type'] = $family->type;

                $hospital = Hospital::query()->where('id', $doctorParent->doctorid)->first();
                $row['is_sign'] = $hospital->is_family;

                $autograph = Autograph::query()->where('userid', $doctorParent->parentid)->first();
                if ($autograph) {
                    $row['endtime'] = date('Y-m-d', strtotime($autograph->endtime));
                    $row['starttime'] = date('Y-m-d', strtotime($autograph->starttime));
                    $row['status'] = $autograph->level;
                    $rs['img'] = str_replace('http://static.i.wedoctors.com.cn/', '', $autograph->img);
                    $rs['created_at'] = $row['created_at'];

                } else {
                    $row['endtime'] = date('Y-m-d', strtotime('+1 year', $doctorParent->createtime));
                    $row['starttime'] = date('Y-m-d', $doctorParent->createtime);
                    $row['status'] = 1;
                }

                $sign = HospitalUserFamily::query()->firstOrCreate( [
                    //'hospital_id' => $doctorParent->doctorid,
                    'user_id' => $doctorParent->parentid,
                    'family_id' => $family->id,
                ],$row);
                if ($autograph) {
                    $rs['hospital_user_family_id'] = $sign->id;
                    \App\Models\Autograph::query()->updateOrCreate( ['hospital_user_family_id' => $sign->id],$rs);
                }
            }
        }
        return 0;
    }
}
